using Newtonsoft.Json;

namespace WebRtcProject.Models.Messages
{
    public abstract class BaseMessage
    {
        public string Id { get; set; } = Guid.NewGuid().ToString();
        public DateTime Timestamp { get; set; } = DateTime.UtcNow;
        public string MessageType { get; set; } = string.Empty;
        public string SourceInstanceId { get; set; } = Environment.MachineName;
        
        public virtual string ToJson()
        {
            return JsonConvert.SerializeObject(this, Formatting.None);
        }
        
        public static T? FromJson<T>(string json) where T : BaseMessage
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(json);
            }
            catch
            {
                return null;
            }
        }
    }
} 